<template>
    <el-dialog :title="form.articleId ? '编辑' : '新增'" v-model="visible"
      :close-on-click-modal="false" draggable>
      <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">

        <el-form-item label="栏目" prop="columnId">
             <el-select v-model="form.columnId" placeholder="请选择栏目">
               <el-option v-for="item in columnList" :key="item.columnId" :label="item.columnName" :value="item.columnId">
               </el-option>
             </el-select>
        </el-form-item>

        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题"/>
        </el-form-item>

        <el-form-item label="副标题" prop="subtitle">
          <el-input v-model="form.subtitle" placeholder="请输入副标题" type="textarea"/>
        </el-form-item>
          <el-form-item label="文章封面" prop="avatar">
            <ImageUpload v-model:imageUrl="form.image" borderRadius="0%">
              <template #empty>
                <el-icon>
                  <Plus/>
                </el-icon>
              </template>
            </ImageUpload>
          </el-form-item>
        <el-form-item label="内容" prop="remark">
          <Editors v-model:getHtml="form.remark" />
        </el-form-item>



      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="onSubmit('0')" :disabled="loading">保存</el-button>
          <el-button type="primary" @click="onSubmit('1')" :disabled="loading">发布</el-button>
          <el-button type="primary" @click="previewDialogRef.previewDialog(form.remark)" :disabled="loading">预览</el-button>
          <el-button @click="visible = false">取消</el-button>

        </span>
      </template>
    </el-dialog>
  <preview-dialog ref="previewDialogRef"></preview-dialog>
</template>

<script setup lang="ts" name="WzArticleDialog">
import { useMessage } from "/@/hooks/message";
import {  addObj,putObj,getObj } from '/@/api/Article/wzArticle'
import { getSonList} from "/@/api/wzjiang/wzColumn";
const emit = defineEmits(['refresh']);
const Editors = defineAsyncComponent(() => import('/@/components/Editor/index.vue'));
const ImageUpload = defineAsyncComponent(() => import('/@/components/Upload/Image.vue'));

const PreviewDialog = defineAsyncComponent(() => import('./component/preview.vue'));
// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)

const previewDialogRef = ref();

// 提交表单数据
const form = reactive({
		articleId:'',
	  title: '',
	  columnId: '',
	  state: '',
	  image: '',
	  remark: '',
	  subtitle: '',
	  // noticeAnnouncement: '0',
	  // photoNews: '0',
	  // recommend: '0',
});

 const columnList = ref();
 const getColumnList = async () => {
   getSonList().then((res: any) => {
     columnList.value = res.data
   })
 }
 getColumnList();

// 定义校验规则
const dataRules = ref({
        columnId: [{required: true, message: '栏目不能为空', trigger: 'blur'}],
        title: [{required: true, message: '标题不能为空', trigger: 'blur'}],
        state: [{required: true, message: '状态不能为空', trigger: 'blur'}],
        remark: [{required: true, message: '内容不能为空', trigger: 'blur'}],
        subtitle: [{required: true, message: '副标题不能为空', trigger: 'blur'}],
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.articleId = ''

  // 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

  // 获取wzArticle信息
  if (id) {
    form.articleId = id
    getwzArticleData(id)
  }
};

// 提交
const onSubmit = async (state: string) => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
    loading.value = true;
    form.state = state;
		form.articleId ? await putObj(form) : await addObj(form);
		useMessage().success(form.articleId ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getwzArticleData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>

